<?php

class UserService extends \BaseService{

    /**
     * 注册用户
     * username 用户名
     * password 用户密码
     * @return [array] [异常信息]
     */
    public static function create($data){
        try{
            $user = Sentry::register(array(
                'username' => $data['username'],
                'password' => $data['password'],
                'activated' => true,
            ));
            
        }catch (Cartalyst\Sentry\Users\LoginRequiredException $e){
            throw new LoginRequiredException;
        }catch (Cartalyst\Sentry\Users\PasswordRequiredException $e){
            throw new PasswordRequiredException;
        }catch (Cartalyst\Sentry\Users\UserExistsException $e){
            throw new UserExistsException;
        }
        return self::getResult('注册成功');
    }

    /**
     * 用户登录
     * username 用户名
     * password 用户密码
     * @return [array] [异常信息]
     */
    public static function login($data){
        try{
            $credentials = array(
                'username' => $data['username'],
                'password' => $data['password'],
            );

            $user = Sentry::authenticate($credentials, false);
        }
        catch (Cartalyst\Sentry\Users\LoginRequiredException $e){
            throw new LoginRequiredException;
        }
        catch (Cartalyst\Sentry\Users\PasswordRequiredException $e){
            throw new PasswordRequiredException;
        }
        catch (Cartalyst\Sentry\Users\WrongPasswordException $e){
            throw new WrongPasswordException;
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
            throw new UserNotFoundException;
        }
        catch (Cartalyst\Sentry\Users\UserNotActivatedException $e){
            throw new notActivatedException;
        }
        return self::getResult('login successful');
    }

}